package com.b2c.oa.controller;

import com.alibaba.fastjson.JSON;
import com.ims.entity.WorkDailyEntity;
import com.ims.service.WorkDailyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RequestMapping("/work")
@Controller
public class WorkDailyController {
    @Autowired
    private WorkDailyService workDailyService;
    /**
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/daily", method = RequestMethod.GET)
    public String list(Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Integer userId = Integer.parseInt(request.getSession().getAttribute("userId").toString());
        var result = workDailyService.getListByUserId(1,10,userId);
        model.addAttribute("pageIndex", 1);
        model.addAttribute("pageSize", 10);
        model.addAttribute("totalSize", result.getTotalSize());
        model.addAttribute("lists", result.getList());

        model.addAttribute("menuId", "work_daily");
        return "work_daily";
    }


    @RequestMapping(value = "/daily_add", method = RequestMethod.GET)
    public String dailyAdd(Model model, HttpServletRequest request) {
        model.addAttribute("menuId", "work_daily");
        return "work_daily_add";
    }

    @RequestMapping(value = "/daily_add", method = RequestMethod.POST)
    public String dailyAddPost(Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Object userId = request.getSession().getAttribute("userId");
//        String date = request.getParameter("date");
        String summary = request.getParameter("summary");
        String plan = request.getParameter("plan");
        String[] tag = request.getParameterValues("tag");
        String suggest = request.getParameter("suggest");

        WorkDailyEntity entity = new WorkDailyEntity();
        entity.setUserId(Integer.parseInt(userId.toString()));
        entity.setPlan(plan);
        entity.setSummary(summary);
        entity.setSuggest(suggest);
        entity.setTag(JSON.toJSONString(tag));
        workDailyService.addDaily(entity);
        return "redirect:/work/daily";
    }
}
